﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;  //对文件进行操作，添加引用System.IO   FileInfo
using System.Windows.Forms;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;


namespace Mygisdzy
{
    public partial class frmClip : Form
    {
        private IMap currentMap;    //当前MapControl控件中的Map对象

        //选定的两个图层的名称
        private string m_SelectedLayerName1; //图层
        private string m_SelectedLayerName2;//图层
        private string m_fileSavePath; //保存Shapefile的路径
        private string m_fileSaveName; //保存Shapefile的名称

        /// <summary>
        /// 获得当前MapControl控件中的Map对象。
        /// </summary>
        public IMap CurrentMap
        {
            set
            {
                currentMap = value;
            }
        }
        public frmClip()
        {
            InitializeComponent();
        }



        private void btnOK_Click(object sender, EventArgs e)
        {
            m_SelectedLayerName1 = comboBox1.Text;
            m_SelectedLayerName2 = comboBox2.Text;

            if (m_SelectedLayerName1 == m_SelectedLayerName2)
            {
                MessageBox.Show("请选择两个不同的图层");
            }
            else if (m_fileSavePath == "" || m_fileSaveName == "")
            {
                MessageBox.Show("请设置输出的路径和图层名称");
            }
            else
            {
                //获得图层和图层
                ILayer pInputLayer = GetLayerByName(m_SelectedLayerName1, currentMap);
                ILayer pClipLayer = GetLayerByName(m_SelectedLayerName2, currentMap);
                if (pInputLayer != null && pClipLayer != null)
                {
                    IFeatureLayer pInputFeaLayer = pInputLayer as IFeatureLayer;
                    IFeatureLayer pClipFeaLayer = pClipLayer as IFeatureLayer;
                    //通过Clip方法进行裁剪操作，返回结果FeatureClass
                    IFeatureClass outFeatureClass = Intersect(pInputFeaLayer.FeatureClass, pClipFeaLayer.FeatureClass, txtSavePath.Text);
                    if (outFeatureClass != null)
                    {
                        ////将结果FeatureClass数据集转为FeatureLayer
                        //IFeatureLayer pFeatueLayer = new FeatureLayerClass();
                        //pFeatueLayer.FeatureClass = outFeatureClass;
                        //pFeatueLayer.Name = outFeatureClass.AliasName;
                        ////将结果数据加载到地图中
                        //currentMap.AddLayer(pFeatueLayer);
                        //this.Close();
                        MessageBox.Show("叠置分析成功");
                    }
                    else
                    {
                        MessageBox.Show("叠置分析失败");
                    }
                }
            }
        }
        private IFeatureClass Intersect(IFeatureClass pSourceFeatureClass, IFeatureClass pTargetFeatureClass, string filePath)
        {
            ITable pSourceTable = pSourceFeatureClass as ITable;
            ITable pTargetTable = pTargetFeatureClass as ITable;

            // IWorkspaceName
            IWorkspaceName pWorkspaceName = new WorkspaceName() as IWorkspaceName;
            pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.ShapefileWorkspaceFactory";
            pWorkspaceName.PathName = System.IO.Path.GetDirectoryName(filePath);

            // IFeatureClassName
            IFeatureClassName pFeatureClassName = new FeatureClassName() as IFeatureClassName;
            pFeatureClassName.ShapeFieldName = "Shape";
            pFeatureClassName.ShapeType = esriGeometryType.esriGeometryPolygon;
            pFeatureClassName.FeatureType = esriFeatureType.esriFTSimple;

            // IDatasetName
            IDatasetName pDatasetName = pFeatureClassName as IDatasetName;
            pDatasetName.WorkspaceName = pWorkspaceName;
            pDatasetName.Name = System.IO.Path.GetFileNameWithoutExtension(filePath);

            // 计算相交部分
            IBasicGeoprocessor pBasicGeoprocessor = new BasicGeoprocessor();
            IFeatureClass pFeatureClass = pBasicGeoprocessor.Intersect(pSourceTable, false, pTargetTable, false, 0.001, pFeatureClassName);
            return pFeatureClass;
        }
   

        private void frmClip_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            comboBox2.Items.Clear();
            for (int i = 0; i < currentMap.LayerCount; i++)
            {
                comboBox1.Items.Add(currentMap.get_Layer(i).Name);
                comboBox2.Items.Add(currentMap.get_Layer(i).Name);
            }
        }


        // 通过图层名称获取目标图层
        private ILayer GetLayerByName(string layerName, IMap pMap)
        {
            // 通过图层名称获取目标图层，传入参数为layerName
            ILayer pLayer = null;
            //遍历MapControl中所有图层，找到与layerName名称相同的图层
            for (int i = 0; i < pMap.LayerCount; i++)
            {
                if (pMap.get_Layer(i).Name == layerName)
                {
                    pLayer = pMap.get_Layer(i);
                    break;
                }
            }
            return pLayer;
        }

        private void btnPathSelect_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "另存shapefile";
            saveFileDialog.Filter = "shapefile(*.shp)|*.shp";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string strFileSavePathAndName = saveFileDialog.FileName;
                txtSavePath.Text = strFileSavePathAndName;
                FileInfo fileInfo = new FileInfo(saveFileDialog.FileName);
                m_fileSaveName = fileInfo.Name;
                m_fileSavePath = fileInfo.DirectoryName;
            }
        }
    }
}

